import 'package:flutter/material.dart';

class FilterChipDemo extends StatefulWidget {
  const FilterChipDemo({Key? key}) : super(key: key);

  @override
  _FilterChipDemoState createState() => _FilterChipDemoState();
}

class _FilterChipDemoState extends State<FilterChipDemo> {
  List<String> _list = ["Apple", "Banana", "Lemon"];

  // 被选中的列表
  List<String> _selectList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FilterChip"),
      ),
      body: Container(
        padding: EdgeInsets.all(15),
        child: Column(children: [
          Text("选择的数据列表:${_selectList.toString()}"),
          Wrap(
            spacing: 8,
            runSpacing: 8,
            children: _list.map((tag) {
              return FilterChip(
                label: Text(tag),
                selected: _selectList.contains(tag),
                onSelected: (choose) {
                  setState(() {
                    if (_selectList.contains(tag)) {
                      _selectList.remove(tag);
                    } else {
                      _selectList.add(tag);
                    }
                  });
                },
              );
            }).toList(),
          )
        ]),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _list = ["Apple", "Banana", "Lemon"];
            _selectList = [];
          });
        },
        child: Icon(Icons.restore),
      ),
    );
  }
}
